<?php
class App_model_category extends Core_model_abstract
{
	public function __construct()
	{
		parent::__construct('category');
	}
	
	protected function _setFields()
	{
		return array(
			'id',
			'cateName',
			'urlName',
			'desc',
			'keyWords',
			'pid',
			'cateKey',
			'goodsQty',
			'createDate',
			'createPerson',
			'modifyDate',
			'modifyPerson'
		);
	}
	
	protected function _setPrimaryKeys()
	{
		return array(
			'id'
		);
	}
	
	protected function _setAutoIncrementFields()
	{
		return array(
			'id'
		);
	}
	
	protected function _setForeignKeyRelation()
	{
		return array();
	}
	
	public function add($cateData)
	{
		$cateData = $this->_getFieldsData($cateData);
		$cateKey = '0';
		if($cateData['pid'] > 0)
		{
			$dbSlave = Core_db_slave::getSlave();
			$pst = $dbSlave->query("SELECT `cateKey` FROM `{$this->_table}` WHERE `id`='{$cateData['pid']}'");
			$parent = $pst->fetch(PDO::FETCH_ASSOC);
			$cateKey = $parent['cateKey'].':'.$cateData['pid'];
		}
		
		$dbMaster = Core_db_master::getMaster();
		$sql = "INSERT INTO `{$this->_table}`(`cateName`,`pid`,`cateKey`,`createDate`) 
		VALUES('{$cateData['cateName']}','{$cateData['pid']}','{$cateKey}','{$cateData['createDate']}')";
		return $dbMaster->exec($sql);
	}
	
	public function getAll()
	{
		$dbSlave = Core_db_slave::getSlave();
		$sql = "SELECT * FROM `{$this->_table}`";
		$statement = $dbSlave->query($sql);
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}

	protected function _validationField($fieldName, $value) 
	{
		return true;
	}

}